<?php

/**
 * JiujiajiuMsgtpl久加久消息模版
 * @author hjc19850430@gmail.com
 */
class JiujiajiuMsgtpl extends BaseMsgtpl
{

	/**
	 * 消息模板通知
	 */
	public function message()
	{
		$data = array(
			'wxuid' => 58334, //线下会员不存在wxuid
			'price' => 478,
			'channel_name' => '宁波桑田店-宁波久加久',
			'add_point' => 478,
			'points' => 0,
		);

		$array = explode('-', $data['channel_name']);
		$array = explode('加盟', $array[0]);
		$array = explode('量贩', $array[0]);
		$data['channel_name'] = $array[0];
		if(mb_substr($data['channel_name'], -1, 1, 'utf8') != '店') {
			$data['channel_name'].='店';
		}

		$uid = 52;
		$token = 'vpwlgz1403009371';

		$this->wxTplSign = 'IvE-YJrJrs7VYdGiwAXOR3-BhmjXmrfGs9PYVHWB5jE';
		$info = $this->getApp($token);
		$Appid = $info['appid'];
		$Appsecret = $info['appsecret'];
		if(empty($Appid) || empty($Appsecret)) {
			return false;
		}
		$openid = M('fans_wx')->where(array('id' => $data['wxuid']))->getField('openid');
		$username = M('fans_wx_info')->where(array('userid' => $data['wxuid']))->getField('username');
		/* 计算总积分  */
// 		$consume=M('consume_log')->where(array('wxuid'=>$data['wxuid'],'create_time'=>array('lt',strtotime(date('Y-m-d')))))->order('id desc')->find();
// 		if(!$consume){
// 			$data['points']=$data['add_point'];
// 		}else{
// 			$points=M('consume_log')->where(array('wxuid'=>$data['wxuid'],'create_time'=>array('egt',strtotime(date('Y-m-d')))))->sum('`add_point`');
// 			$data['points']=$consume['points']+$consume['add_point']+$points;
// 		}
		$send = array(
			'first' => array("value" => urlencode('尊敬的久加久会员' . $username . '，最新交易提醒：'), "color" => "#173177"),
			'time' => array('value' => urlencode(date('Y年m月d日')), "color" => "#173177"),
			'org' => array('value' => urlencode($data['channel_name']), "color" => "#173177"),
			'type' => array('value' => urlencode('酒水'), "color" => "#173177"),
			'money' => array('value' => urlencode($data['price'] . '元'), "color" => "#173177"),
			'point' => array('value' => urlencode($data['add_point'] . '分'), "color" => "#173177"),
			'remark' => array('value' => urlencode("查询积分总数，请点击菜单栏“会员卡”或回复关键词“会员卡”。"), "color" => "#173177"),
		);

		$result = $this->wxObj->messageTpl($Appid, $Appsecret, 'oQKbujmK2giOKG0a8PtuyN68_X4E', $this->wxTplSign, $send);
		$insert = array(
			'uid' => $uid,
			'token' => $token,
			'openid' => $openid,
			'message' => '久加久线下消费提醒',
			'template' => $template_id,
			'send' => serialize($send),
			'result' => serialize($result),
			'create_time' => time(),
		);
		M('message_tpl_wx')->add($insert);
		return 'S';
	}

}
